﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 易容酒店管理系统
{
    public partial class Check : Form
    {
        public Check()
        {
            InitializeComponent();
        }
        public int NumberID = 0;
        private void Check_Load(object sender, EventArgs e)
        {
            panel1.BackColor = Color.FromArgb(254, 133, 77);
            cobPapers.Text = "身份证";
            COMTYPE();
            COMMETHOD();
        }
        //关闭
        private void picColse_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void butClear_Click(object sender, EventArgs e)
        {
            txtID.Text = "";
            txtName.Text = "";
            txtTelephone.Text = "";
            cobPapers.Text = "身份证";
            text.Text = "";
        }

        private void txtName_Leave(object sender, EventArgs e)
        {
            txtName2.Text = txtName.Text;
        }
        
        
        /// <summary>
        /// 返回所选择的客服类型
        /// </summary>
        /// <returns></returns>
        public string  Type() 
        {
            string type = "";
            type = comType.Text;
            return type;
            
        }

        //入住
        private void butEntry_Click(object sender, EventArgs e)
        {
            DateTime  currentTime = DateTime.Now; 
            int year = currentTime.Year;
            int Month = currentTime.Month;
            int inDay = currentTime.Day;
            NumberID++;
            string ID = "";
            if (Month > 10)
            {
                if (inDay > 10)
                {
                    ID = year.ToString() + "0" + Month.ToString() + "0" + inDay.ToString();
                }
                else
                {
                    ID = year.ToString() + "0" + Month.ToString() + inDay.ToString();
                }
            }
            else
            {
                if (inDay > 10)
                {
                    ID = year.ToString() +  Month.ToString() + "0" + inDay.ToString();
                }
                else
                {
                    ID = year.ToString() + Month.ToString() + inDay.ToString();
                }
            }
            string InTime = currentTime.ToString("yyyy-MM-dd");
            DateTime OutTime = dateTimePicker1.Value;
            int Number = 0;
            if (Convert.ToInt32(currentTime.ToString("dd")) == Convert.ToInt32(OutTime.ToString("dd")))
            {
                Number = 0;
            }
            Number = Convert.ToInt32(currentTime.ToString("dd")) - Convert.ToInt32(OutTime.ToString("dd"));
            Number *= -1;
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "insert into Guest_Room_Number values";

                Sql += "('" + ID + Number.ToString() + "','" + ID + Number.ToString() + "','" + Room() + "','" + Type() + "','" + txtName.Text + "','" + txtTelephone.Text + "','" + InTime + "','" + OutTime.ToString("yyyy-MM-dd") + "','" + Number.ToString() + "','" + Money().ToString() + "','','');";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        // MessageBox.Show(Number.ToString());
                        MessageBox.Show("登记完成房间号为：" + Room());

                        this.Close();
                    }
                    //else if (Type1)
                    //{
                    //    MessageBox.Show("入住方式未选择！");
                    //}
                    //else if (Type2)
                    //{
                    //    MessageBox.Show("客房类型未选择！");
                    //}
                    else
                    {
                        MessageBox.Show("请选择客房类型与入住方式");
                    }
                    con.Close();
                }
                string Sql2 = "insert into Customer_data values('" + txtName.Text + "','" + cobPapers.Text + "','" + txtID.Text + "','" + txtTelephone.Text + "','" + text.Text + "','" + InTime + "','" +comType.Text+"','"+ comMeoth.Text + "')";
                using (SqlCommand cmd2 = new SqlCommand(Sql2, con))
                {
                    con.Open();
                    int num = cmd2.ExecuteNonQuery();
                    if (num > 0)
                    {
                        this.Close();
                    }
                }
            }
            this.Close();
            Method.Bool2 = true;

        }
        /// <summary>
        /// 返回入住方式
        /// </summary>
        /// <returns></returns>
        private string Mode()
        {
            string mode = "";
            mode = comMeoth.Text;
            return mode;
        }
        /// <summary>
        /// 分配客房
        /// </summary>
        /// <returns></returns>
        private string Room()
        {
            string ID = "";
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                //string.Format("SELECT * FROM %s", Type());
                string Sql = "select top 1 RoomID from GuestInfo where Type='" + Type() + "'and GusetInfo='无' and setting='已打扫' and Reserve='未预订';";
                using (SqlCommand cmd = new SqlCommand(Sql,con))
                {
                    con.Open();
                    ID = (string)cmd.ExecuteScalar();
                }
            }
            return ID;
        }
        /// <summary>
        /// 获取房务押金
        /// </summary>
        /// <returns></returns>
        public decimal Money()
        {
            decimal money;
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "select Money from Guest_Room where GuestType='" + comType.Text + "';";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    money = Convert.ToDecimal(cmd.ExecuteScalar());
                }
            }
            return money;
        }
        /// <summary>
        /// 读取所设定的客房类型
        /// </summary>
        public void COMTYPE()
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from Guest_Room;";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "Guest_Room");
            DataTable table = set.Tables["Guest_Room"];

            comType.DisplayMember = "GuestType";
            comType.DataSource = set.Tables["Guest_Room"];
        }
        /// <summary>
        /// 获取设定的入住方式
        /// </summary>
        public void COMMETHOD()
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from Method;";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "Method");
            DataTable table = set.Tables["Method"];

            comMeoth.DisplayMember = "Method";
            comMeoth.DataSource = set.Tables["Method"];
        }

    }
}
